package com.maiji.cloud.controller;

import com.maiji.cloud.entities.community.ComMsgInfo;
import com.maiji.cloud.entities.community.ComMsgTransmitRule;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.BaseInputDto;
import com.maiji.cloud.request.IdInputDto;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.community.ComMsgTopManInputDto;
import com.maiji.cloud.request.community.ComPubComInputDto;
import com.maiji.cloud.request.community.ComRecsInputDto;
import com.maiji.cloud.request.community.ComSearchInputDto;
import com.maiji.cloud.response.*;
import com.maiji.cloud.response.community.ComDetailOutputDto;
import com.maiji.cloud.response.community.ComMsgBaseOutputDto;
import com.maiji.cloud.response.community.ComRecsOutputDto;
import com.maiji.cloud.response.community.CommunityStatisticsOutputDto;
import com.maiji.cloud.response.shopingmall.FinancialDetailsResData;
import com.maiji.cloud.service.CommunityService;
import com.maiji.cloud.utils.Unlogin;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/community")
@Api(value = "论坛模块")
public class CommunityController {

    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private CommunityService communityService;

    @PostMapping("/publish")
    @ApiOperation("发布动态")
    public BaseOutPutDto<ComRecsOutputDto>publish(@RequestBody BaseInputDto<ComPubComInputDto> inputDto, @RequestHeader("maijiToken")String maijiToken){
        return communityService.publishCom(inputDto,maijiToken);
    }

    @PostMapping("/detailMsg")
    @ApiOperation("动态详细")
    public BaseOutPutDto<ComDetailOutputDto> detailMsg(@RequestBody BaseInputDto<IdInputDto> inputDto,@RequestHeader(value = "maijiToken",required =false)String maijiToken){
        return communityService.detailMsg(inputDto,maijiToken);
    }

    @PostMapping("/caseList")
    @ApiOperation("案例列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> caseList(@RequestBody BaseInputDto<ComRecsInputDto> inputDto, @RequestHeader(value="maijiToken",required =false)String maijiToken){

        return communityService.caseList(inputDto,maijiToken);
    }

    @PostMapping("/recommends")
    @ApiOperation("推荐列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> recommends(@RequestBody BaseInputDto<ComRecsInputDto> inputDto, @RequestHeader(value = "maijiToken",required =false)String maijiToken){

        return communityService.recommends(inputDto,maijiToken);
    }

    @PostMapping("/commonMsgs")
    @ApiOperation("普通动态列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> commonMsgs(@RequestBody BaseInputDto<ComRecsInputDto> inputDto,@RequestHeader(value = "maijiToken",required =false)String maijiToken){

        return communityService.commonMsgs(inputDto,maijiToken);
    }

    @PostMapping("/msgList")
    @ApiOperation("社区通用列表查询")
    public BaseOutPutDto<List<ComRecsOutputDto>> msgList(@RequestBody BaseInputDto<ComRecsInputDto> inputDto,
                                                         @RequestHeader(name = "maijiToken", required = false)String maijiToken){
        return communityService.msgList(inputDto,maijiToken);
    }

    @PostMapping("/focusersBlogList")
    @ApiOperation("关注动态列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> focusersBlogList(@RequestBody BaseInputDto<ComRecsInputDto> inputDto, @RequestHeader("maijiToken")String maijiToken){

        return communityService.focusersBlogList(inputDto,maijiToken);
    }

    @PostMapping("/userHotList")
    @ApiOperation("用户被推荐过的动态列表 priority 优先级>=1")
    public BaseOutPutDto<List<ComRecsOutputDto>> userHotList(@RequestBody BaseInputDto<IdInputDto> inputDto,@RequestHeader("maijiToken")String maijiToken){
        return communityService.userHotList(inputDto,maijiToken);
    }

    @PostMapping("/mineBlogList")
    @ApiOperation("我的动态列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> mineBlogList(@RequestBody BaseInputDto inputDto,@RequestHeader("maijiToken")String maijiToken){
        return communityService.mineBlogList(inputDto,maijiToken);
    }

    @PostMapping("/userBlogList")
    @ApiOperation("用户动态列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> userBlogList(@RequestBody BaseInputDto<IdInputDto> inputDto,@RequestHeader("maijiToken")String maijiToken){
        return communityService.userBlogList(inputDto,maijiToken);
    }

    @PostMapping("/userCollectList")
    @ApiOperation("用户社区收藏列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> userCollectList(@RequestBody BaseInputDto inputDto,@RequestHeader("maijiToken")String maijiToken){
        return communityService.userCollectList(inputDto,maijiToken);
    }

    @PostMapping("/topList")
    @ApiOperation("社区置顶列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> topList(@RequestBody BaseInputDto inputDto,@RequestHeader(value = "maijiToken",required =false)String maijiToken){
        return communityService.topList(inputDto , maijiToken);
    }

    @PostMapping("/shareSuc")
    @ApiOperation("分享成功调用")
    public BaseOutPutDto<FinancialDetailsResData> shareSuc(@RequestBody BaseInputDto<IdInputDto> inputDto, @RequestHeader("maijiToken")String maijiToken){
        return communityService.shareSuc(inputDto,maijiToken);
    }

    @PostMapping("/searchMsg")
    @ApiOperation("搜索动态")
    public BaseOutPutDto<List<ComRecsOutputDto>> searchMsg(@RequestBody BaseInputDto<ComSearchInputDto> inputDto, @RequestHeader("maijiToken")String maijiToken){
        return communityService.searchMsg(inputDto,maijiToken);
    }

    @PostMapping("/delete")
    @ApiOperation("删除动态")
    public BaseOutPutDto<String>delete(@RequestBody BaseInputDto<IdInputDto> inputDto,@RequestHeader("maijiToken")String maijiToken){
        return communityService.delete(inputDto,maijiToken);
    }

    @PostMapping("/managerDelete")
    @ApiOperation("管理员删除动态")
    public BaseOutPutDto<String>managerDelete(@RequestBody BaseInputDto<IdInputDto> inputDto){
        return communityService.managerDelete(inputDto);
    }

    /**
     * 动态设置为精选
     * @param inputDto
     * @return
     */
    @PostMapping("/setEssence")
    @ApiOperation("动态设置为精选")
    public BaseOutPutDto<String>setEssence(@RequestBody BaseInputDto<IdInputDto> inputDto){

        return communityService.setEssence(inputDto);
    }

    @PostMapping("/setCase")
    @ApiOperation("动态设置为案例")
    public BaseOutPutDto<String>setCase(@RequestBody BaseInputDto<IdInputDto> inputDto){

        return communityService.setCase(inputDto);
    }

    /**
     * 管理消息置顶
     * @param inputDto
     * @return
     */
    @PostMapping("/topListManager")
    @ApiOperation("管理消息置顶")
    public BaseOutPutDto<String> topListManager(@RequestBody BaseInputDto<ComMsgTopManInputDto>inputDto){
        return communityService.topListManager(inputDto);
    }

    @PostMapping("/setIntegral")
    @ApiOperation("设置转发得积分")
    public BaseOutPutDto<String> setIntegral(BaseInputDto<IdInputDto> inputDto){
        return communityService.setIntegral(inputDto);
    }

    @PostMapping("/cancelIntegral")
    @ApiOperation("取消转发积分")
    public BaseOutPutDto<String> cancelIntegral(BaseInputDto<IdInputDto> inputDto){
        return communityService.cancelIntegral(inputDto);
    }

    @PostMapping("/integralList")
    @ApiOperation("可转发得积分列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> integralList(@RequestBody BaseInputDto<ComRecsInputDto> inputDto,@RequestHeader(value = "maijiToken",required =false)String maijiToken){
        return communityService.integralList(inputDto,maijiToken);
    }

    @PostMapping("/unionMsgList")
    @ApiOperation("发现 、 精选 、 积分通用接口")
    public BaseOutPutDto<List<ComRecsOutputDto>> unionMsgList(@RequestBody BaseInputDto<ComRecsInputDto> inputDto,@RequestHeader(value = "maijiToken",required =false)String maijiToken){
        return communityService.unionMsgList(inputDto,maijiToken);
    }
    
    /**
     * 根据用户ID统计用户粉丝/关注/发布的动态数量
     * @return
     */
    @PostMapping("/communityStatisticsQuantity")
    public BaseOutPutDto<CommunityStatisticsOutputDto> communityStatisticsQuantity(@RequestBody BaseInputDto<String> inputDto){
		
    	if(StringUtils.isBlank(inputDto.getData())) return new BaseOutPutDto<CommunityStatisticsOutputDto>(Status.PARAMETERERROR);
    	
    	return communityService.communityStatisticsQuantity(inputDto.getData());
    }

    /**
     * 获取动态列表
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("/findAllComMsgInfos")
    public BaseMetaResDto<List<ComMsgInfo>> findAllComMsgInfos (@RequestBody BaseDataReqDto<ComMsgInfo> baseDataReqDto) {
        Integer priority = baseDataReqDto.getData().getPriority();
        String nickName = baseDataReqDto.getData().getNickName();
        String content = baseDataReqDto.getData().getContent();
        String act = baseDataReqDto.getData().getAct();
        Boolean isolation = baseDataReqDto.getData().getIsolation();
        List<Long> publishTimes = baseDataReqDto.getData().getPublishTimes();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        if (Arrays.asList(1, 2, 3).contains(priority)) return communityService.findAllMidComMsgInfos(priority, nickName, content, act,isolation, publishTimes, metaData);
        else return communityService.findAllComMsgInfos(priority, nickName, content, act,isolation, publishTimes, metaData);
    }

    /**
     * 编辑动态
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("/editComMsgInfo")
    public BaseResDto editComMsgInfo (@RequestBody BaseDataReqDto<ComMsgInfo> baseDataReqDto) {
        ComMsgInfo comMsgInfo = baseDataReqDto.getData();
        if (StringUtil.isBlank(comMsgInfo.getMsgId())) return new BaseResDto(Status.ERROR);
        return communityService.editComMsgInfo(comMsgInfo);
    }

    /**
     * 获取动态详细信息
     * @param baseDataReqDto
     * @return
     */
    @PostMapping("/findComMsgById")
    public BaseDataResDto<ComMsgBaseOutputDto> findComMsgById (@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String msgId = baseDataReqDto.getData();
        return communityService.findComMsgById(msgId);
    }

    /**
     * 根据活动搜索动态列表 ， 置顶独立
     * @param inputDto
     * @param maijiToken
     * @return
     */
    @PostMapping("/findMsgsByActName")
    @ApiOperation("根据活动搜索动态列表 ， 置顶独立")
    public BaseOutPutDto<List<ComRecsOutputDto>> findMsgsByActName(@RequestBody BaseInputDto<ComRecsInputDto> inputDto,@RequestHeader(value = "maijiToken",required =false)String maijiToken){
        return communityService.findMsgsByActName(inputDto, maijiToken);
    }

    /**
     * 首页列表
     * @param inputDto
     * @param maijiToken
     * @return
     */
    @PostMapping("/homePageList")
    @ApiOperation("首页列表")
    public BaseOutPutDto<List<ComRecsOutputDto>> homePageList(@RequestBody BaseInputDto<ComRecsInputDto> inputDto,@RequestHeader(value = "maijiToken",required =false)String maijiToken){
        return communityService.homePageList(inputDto,maijiToken);
    }

    /**
     * 更新转发得积分基本规则
     * @param inputDto
     * @return
     */
    @PostMapping("/updMsgTransmitRule")
    @ApiOperation("更新转发得积分基本规则")
    public BaseOutPutDto updMsgTransmitRule (@RequestBody BaseInputDto<ComMsgTransmitRule> inputDto ){
        return communityService.updMsgTransmitRule(inputDto);
    }

    /**
     * 获取转发得积分基本规则
     * @return
     */
    @PostMapping("/findMsgTransmitRule")
    @ApiOperation("获取转发得积分基本规则")
    public BaseDataResDto<ComMsgTransmitRule> findMsgTransmitRule (){
        return communityService.findComMsgTransmitRule();
    }

    /**
     * 编辑转发得积分基本规则
     * @return
     */
    @PostMapping("/editComMsgTransmitRule")
    @ApiOperation("编辑转发得积分基本规则")
    public BaseResDto editComMsgTransmitRule (@RequestBody BaseDataReqDto<ComMsgTransmitRule> baseDataReqDto){
        ComMsgTransmitRule data = baseDataReqDto.getData();
        return communityService.editComMsgTransmitRule(data);
    }

    /**
     * 获取转发得积分基本规则编辑记录
     * @return
     */
    @PostMapping("/findAllComMsgTransmitRuleLogs")
    @ApiOperation("获取转发得积分基本规则编辑记录")
    public BaseDataResDto<List<ComMsgTransmitRule>> findAllComMsgTransmitRuleLogs (@RequestBody BaseDataReqDto<List<Date>> baseDataReqDto){
        List<Date> data = baseDataReqDto.getData();
        return communityService.findAllComMsgTransmitRuleLogs(data);
    }

}

